/*
 * the codes was writted by taing, but you can copy or moidfy it
 */

package com.taing.autosqlutils;

import com.taing.AutoSqlIntercepter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;


/**
 * @Author tianyi
 * @Description 这个是mybatis的配置
 * @Date 2023 - 02 - 01
 **/
@Configuration
public class MybatisConfig {
    @Bean("sqlSessionFactory")
    @Primary
    public SqlSessionFactory masterSqlSessionFactory(DataSource ds) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(ds);

        String locationPattern = "classpath:com/taing/autosql/sqlconfig/IAutoSqlDao.xml";
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(locationPattern));

        String typeAliasesPackeage = "com.taing.autosql.model";
        sqlSessionFactoryBean.setTypeAliasesPackage(typeAliasesPackeage);

        sqlSessionFactoryBean.setPlugins(new AutoSqlIntercepter());

        return sqlSessionFactoryBean.getObject();
    }

}
